package com.expense.common.model.auth;

import cn.hutool.core.util.StrUtil;
import com.expense.common.model.base.BaseModel;
import lombok.Data;
import org.beetl.sql.core.annotatoin.Table;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/* 
* 
* gen by beetlsql 2018-09-12
*/
@Data
@Table(name="admin_menu")
public class AdminMenu  extends BaseModel {
	
	/*
	当前生成的code数
	*/
	private Integer codeNum ;
	/*
	是否初始化
	*/
	private Integer isInit ;
	/*
	是否展示该业务 0：是；1否
	*/
	private Boolean isHidden ;
	private Integer level ;
	private String code ;
	private String parentCode;
	/*
	生成code日期
	*/
	private String codeDate ;
	private String redirect ;
	private String iconCls ;
	private Integer isLeaf;
	private String name ;
	private String prefix ;
	private String remark ;
	private Long showOrder ;
	private String path ;
	private String pageName ;
	private String component ;
	private String title ;
	private Boolean noCache ;

	private  String parentTitle;

	private List<AdminMenu> children;

	public static List<AdminMenu> generateListTree(List<AdminMenu> menuList,String pCode) {
		List<AdminMenu> list = new ArrayList<>();
		if(menuList != null && menuList.size()>0){
			Map<Boolean, List<AdminMenu>> streamMap = menuList.parallelStream().collect(Collectors.partitioningBy(t -> t.getParentCode().equals(pCode)));
			list = streamMap.get(true);
			List<AdminMenu> other = streamMap.get(false);
			if(list.size()>0) {
				for (AdminMenu menu : list) {
					if(StrUtil.isBlank(menu.title)) menu.setTitle("无标题");
					List<AdminMenu> children = generateListTree(other, menu.getCode());
					if(children.size()==0) children = null;
					menu.setChildren(children);
				}
			}
		}
		return list;
	}
}
